//package com.corpgovernment.organization.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.corpgovernment.common.apollo.PayPlatformApollo;
//import com.corpgovernment.common.constant.CommonConst;
//import com.corpgovernment.common.datasource.DynamicDataSource;
//import com.corpgovernment.common.holder.TenantContextHolder;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.sql.DataSource;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author jhxue
// */
//@Configuration
//@Slf4j(topic = "MultiTenantDataSourceConfig")
//public class MultiTenantDataSourceConfig {
//
//
//	@Autowired
//	private PayPlatformApollo payPlatformApollo;
//
//	@Bean("dynamicDataSource")
//	public DataSource dynamicDataSource() {
//		List<String> tenantList = payPlatformApollo.getTenantList();
//		DruidDataSource defaultDataSource = this.getDefaultDataSource();
//		Map<Object, Object> dataSourceMap = new HashMap<>(16);
//		dataSourceMap.put(CommonConst.DEFAULT_TENANT_ID, defaultDataSource);
//		tenantList.forEach(tenantId -> {
//			try {
//				TenantContextHolder.setTenantId(tenantId);
//				DruidDataSource ds = this.getDataSource();
//				dataSourceMap.put(tenantId, ds);
//			} finally {
//				TenantContextHolder.clearTenantId();
//			}
//		});
//		log.info("已加载租户库数据源" + tenantList);
//		return DynamicDataSource.create(defaultDataSource, dataSourceMap);
//	}
//
//	private DruidDataSource getDataSource() {
//		DruidDataSource ds = new DruidDataSource();
//		ds.setDriverClassName(payPlatformApollo.getDriverClassName());
//		ds.setUrl(payPlatformApollo.getDatabaseUrl());
//		ds.setUsername(payPlatformApollo.getDatabaseUsername());
//		ds.setPassword(payPlatformApollo.getDatabasePassword());
//		ds.setInitialSize(payPlatformApollo.getDatabaseInitialSize());
//		ds.setMaxActive(payPlatformApollo.getDatabaseMaxActive());
//		ds.setMinIdle(payPlatformApollo.getDatabaseMinIdle());
//		ds.setMaxWait(payPlatformApollo.getDatabaseMaxWait());
//		return ds;
//	}
//
//
//	private DruidDataSource getDefaultDataSource() {
//		try {
//			TenantContextHolder.setTenantId(CommonConst.DEFAULT_TENANT_ID);
//			return this.getDataSource();
//		} finally {
//			TenantContextHolder.clearTenantId();
//		}
//	}
//
//}
